package com.quanyan.authority.web;

import com.lefu.commons.utils.Page;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.authority.model.request.ReqBusiness;
import com.quanyan.authority.service.BusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/4.
 */
@RestController
@RequestMapping("/admin/api/authority/business/api/")
public class BusinessController extends BaseController<ReqBusiness>{

    private static final Logger logger = LoggerFactory.getLogger(SystemController.class);

    @Autowired
    private BusinessService businessService;

    @Autowired
    private HttpServletRequest request;


    @Override
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public APIResponse save(ReqBusiness reqBusiness, BindingResult bindingResult) {
        if(bindingResult.hasErrors())
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        try {
            int uid = (Integer)request.getAttribute("adminUid");
            reqBusiness.setFounder(uid);
            businessService.add(reqBusiness);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @Override
    @RequestMapping(value = "query",method = RequestMethod.POST)
    public APIResponse query(ReqBusiness reqBusiness) {
        try {
            Page page = businessService.findAll(reqBusiness, reqBusiness);
            return APIResponse.returnSuccess(page);
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @Override
    @RequestMapping(value = "queryById",method = RequestMethod.POST)
    public APIResponse queryById(Map map) {
        try {
            Integer id = (Integer) map.get("id");
            if(id == null)
                return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
            return APIResponse.returnSuccess(  businessService.findById(id));
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @Override
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public APIResponse update(ReqBusiness reqBusiness) {
        if(reqBusiness.getId() == null)
            return APIResponse.returnFail("id不能为空！");
        try {
            businessService.update(reqBusiness);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @Override
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public APIResponse deleteById(Map map) {
        try {
            Integer id = (Integer) map.get("id");
            if(id == null)
                return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
            businessService.del(id);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("{}", e);
            return APIResponse.returnFail("系统异常！");
        }
    }
}
